<?php

class Video extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    function index() {
        $this->load->Model('Course_Permission_Model');
        $courseList = $this->Course_Permission_Model->getDDLCourseByMemberID($this->session->userdata('memberID'));
        $this->data['courseList'] = $courseList;
//        $courseID = array();
//        
//        //get member ID
//        $memberID = $this->session->userdata('memberID');
//        
//        //get course permission by memberid
//        $this->load->model('Course_Permission_Model');
//        $courseList = $this->Course_Permission_Model->getCourseByMemberID($memberID);
//        
//        //set course id to couesr array
//        for($i=0;$i<count($courseList);$i++){
//            $courseID[$i] = $courseList[$i]->COURSE_ID;
//        }
//        
//        $this->load->model('Video_Model');
//        $query = $this->Video_Model->getAllVideoByCourse(10, $this->uri->segment(3), $courseID);
//
//        $result = $query->result();
//        
        $this->load->library('pagination');
        $this->load->library('table');
//        
        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/Video/index';
        $config['total_rows'] = 10;
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
//        
        $this->pagination->initialize($config);

        $this->table->set_heading('No.', 'Course Name', 'Video Name', 'Video Detail', 'Manage'); //Create columns
        $cell = array('data' => 'No results found', 'colspan' => 5, 'align' => 'center');
        $this->table->add_row($cell);
//        
//        if($query->num_rows() > 0)
//        {   
//            for($i=0;$i<count($result);$i++):
//                $row = $result[$i];
//                $this->table->add_row(
//                        $i+1, 
//                        $row->NAME, 
//                        $row->VIDEO_NAME, 
//                        $row->VIDEO_DETAIL,
//                        anchor( base_url().'index.php/Video/WatchVideo/'.$row->ID, 'link')
//                        ); 
//            endfor;
//
//        }else{
//            $this->table->add_row('No results found','','','');
//        }
//
        $tmpl = array(
            'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
            'heading_cell_start' => '<th class="table-header-repeat line-left"><a href>',
            'heading_cell_end' => '</a></th>',
            'table_close' => '</table>');
//
        $this->table->set_template($tmpl);
        
        $this->data['main_title'] = 'Watch Video';
        $this->data['action'] = '';
        $this->data['main_content'] = 'video';
        $this->load->view('includes/template', $this->data);
    }

    function WatchVideo($videoID) {
        //set times of user
        $this->load->Model('Video_Model');
        $video = $this->Video_Model->getVideoByID($videoID);

        //get course id from video
        if ($video->num_rows() == 1) {
            $row = $video->row();
            $courseID = $row->COURSE_ID;
        }
        
        $times = null;
        
        $this->load->Model('Course_Permission_Model');
        $vTimes = $this->Course_Permission_Model->getRecordByKey($courseID, $this->session->userdata('memberID'));
        
        //get course permission and decrease video times
        $query = $this->Course_Permission_Model->getRecordByKey($courseID, $this->session->userdata('memberID'));
        if($query != null):
            $times = $query->TIMES - 1;
            $this->Course_Permission_Model->updatePermissionTimes($times, $this->session->userdata('memberID'), $courseID);
        endif;
        
        //delete if over limit permission
        if($times == 0 || $times == null): 
            $this->db->delete('course_permission', array('MEMBER_ID' => $this->session->userdata('memberID'), 'COURSE_ID' => $courseID));
        endif;
        
        if($vTimes != null):
            $this->data['vTimes'] = $vTimes->TIMES;
        else:
            $this->data['vTimes'] = 0;
        endif;
        
        $this->data['main_content'] = 'watch_video';
        $this->load->view('includes/template', $this->data);
    }

    function searchVideo() {
        $this->load->library('pagination');
        $this->load->library('table');


        $config['base_url'] = 'http://localhost/AsNew/ci/index.php/ViewVideo/index';
        $config['total_rows'] = $this->db->get('VIDEO')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 20;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);

        $search_course = $this->input->post('search_course');
        $search_videoname = $this->input->post('search_videoname');

        $this->load->model('Video_Model');
        $query = $this->Video_Model->searchVideo($search_course, $search_videoname, $config['per_page'], $this->uri->segment(3));
        $result = $query->result();

        $this->load->model('Course_Permission_Model');
        $coursePermission = $this->Course_Permission_Model->getRecordByKey($search_course, $this->session->userdata('memberID'));

        $this->table->set_heading('No', 'Video Name', 'Video Detail', 'Video Path', 'Manage'); //Create columns

        if ($coursePermission) {
            if ($coursePermission->TIMES == 0) {
                $cell = array('data' => 'Your permission limit is zero(Please contact Admin)', 'colspan' => 5, 'align' => 'center', 'color' => 'red');
                $this->table->add_row($cell);
            } else {
                if ($query->num_rows() > 0) {
                    for ($i = 0; $i < count($result); $i++):
                        $row = $result[$i];
                        $this->table->add_row(
                                $i + 1, $row->VIDEO_NAME, $row->VIDEO_DETAIL, $row->VIDEO_PATH, anchor(base_url() . 'index.php/Video/WatchVideo/' . $row->ID, '<img src="' . base_url() . 'images/view.png" alt="View" />')
                        );
                    endfor;
                }else {
                    $cell = array('data' => 'No results found', 'colspan' => 5, 'align' => 'center');
                    $this->table->add_row($cell);
                }
            }
        } else {
            $cell = array('data' => 'Your permission limit is zero(Please contact Admin)', 'colspan' => 5, 'align' => 'center', 'style' => 'color:red');
            $this->table->add_row($cell);
        }


        $tmpl = array(
            'table_open' => '<table border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">',
            'heading_cell_start' => '<th class="table-header-repeat line-left"><a href>',
            'heading_cell_end' => '</a></th>',
            'table_close' => '</table>');

        $this->table->set_template($tmpl);

//        $this->load->Model('Course_Model');
//        $courseList = $this->Course_Model->getCourseList();
//        $this->data['courseList'] = $courseList;
        $this->load->Model('Course_Permission_Model');
        $courseList = $this->Course_Permission_Model->getDDLCourseByMemberID($this->session->userdata('memberID'));
        $this->data['courseList'] = $courseList;

        $this->data['main_title'] = 'Watch Video';
        $this->data['action'] = '';
        $this->data['main_content'] = 'video';
        $this->load->view('includes/template', $this->data);
    }

}

?>
